package com.wave.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.wave.common.utils.StringUtils;
import com.wave.common.core.page.TableDataInfo;
import com.wave.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.wave.system.domain.bo.SysToolBoardLineBo;
import com.wave.system.domain.vo.SysToolBoardLineVo;
import com.wave.system.domain.SysToolBoardLine;
import com.wave.system.mapper.SysToolBoardLineMapper;
import com.wave.system.service.ISysToolBoardLineService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 工装板线体Service业务层处理
 *
 * @author wave
 * @date 2024-10-12
 */
@RequiredArgsConstructor
@Service
public class SysToolBoardLineServiceImpl implements ISysToolBoardLineService {

    private final SysToolBoardLineMapper baseMapper;

    /**
     * 查询工装板线体
     */
    @Override
    public SysToolBoardLineVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询工装板线体列表
     */
    @Override
    public TableDataInfo<SysToolBoardLineVo> queryPageList(SysToolBoardLineBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysToolBoardLine> lqw = buildQueryWrapper(bo);
        Page<SysToolBoardLineVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工装板线体列表
     */
    @Override
    public List<SysToolBoardLineVo> queryList(SysToolBoardLineBo bo) {
        LambdaQueryWrapper<SysToolBoardLine> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysToolBoardLine> buildQueryWrapper(SysToolBoardLineBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysToolBoardLine> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getLineNode()), SysToolBoardLine::getLineNode, bo.getLineNode());
        lqw.eq(StringUtils.isNotBlank(bo.getProductSn()), SysToolBoardLine::getProductSn, bo.getProductSn());
        lqw.eq(StringUtils.isNotBlank(bo.getTargetNode1()), SysToolBoardLine::getTargetNode1, bo.getTargetNode1());
        lqw.eq(StringUtils.isNotBlank(bo.getTargetNode2()), SysToolBoardLine::getTargetNode2, bo.getTargetNode2());
        return lqw;
    }

    /**
     * 新增工装板线体
     */
    @Override
    public Boolean insertByBo(SysToolBoardLineBo bo) {
        SysToolBoardLine add = BeanUtil.toBean(bo, SysToolBoardLine.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改工装板线体
     */
    @Override
    public Boolean updateByBo(SysToolBoardLineBo bo) {
        SysToolBoardLine update = BeanUtil.toBean(bo, SysToolBoardLine.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysToolBoardLine entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除工装板线体
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
